package com.xingu.xg.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xingu.xg.common.consts.ComRT;
import com.xingu.xg.common.consts.ComResult;
import com.xingu.xg.dto.AddNodeQuery;
import com.xingu.xg.dto.CopyNodeQuery;
import com.xingu.xg.dto.DeleteNodeQuery;
import com.xingu.xg.dto.NodesDto;
import com.xingu.xg.pojo.Nodes;
import com.xingu.xg.service.NodesService;
import com.xingu.xg.utils.MenuTree;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @descriptions: TODO
 * @author: 涵哥
 * @date: 2024/3/11 14:58
 */
@RestController
@RequestMapping("/nodes")
@Api(tags = "树图节点接口")
public class NodesController {

    @Autowired
    private NodesService nodesService;


    @ApiOperation(value = "查询全部节点", notes = "获取所有节点信息")
    @GetMapping("/tree")
    public ComResult<?> tree() {
        List<NodesDto> tree = MenuTree.makeTree(nodesService.list(), 0);
        tree.sort(Comparator.comparingInt(NodesDto::getSortOrder));
        return ComRT.success(tree);
    }

    @ApiOperation(value = "节点向上移动", notes = "将指定节点向上移动一个位置")
    @PostMapping("/up")
    public ComResult<?> nodesUp(@RequestBody  @Validated AddNodeQuery addNodeQuery) {
        return nodesService.nodesUp(Integer.valueOf(addNodeQuery.getSelectedId()));
    }

    @ApiOperation(value = "节点向下移动", notes = "将指定节点向下移动一个位置")
    @PostMapping("/down")
    public ComResult<?> nodesDown(@RequestBody  @Validated AddNodeQuery addNodeQuery) {
        return nodesService.nodesDown(Integer.valueOf(addNodeQuery.getSelectedId()));
    }

    @ApiOperation(value = "创建子节点", notes = "在指定父节点下创建一个新的子节点")
    @PostMapping("/create-child")
    public ComResult<?> createChildNode(@RequestBody @Validated AddNodeQuery addNodeQuery) {
        return nodesService.createChildNode(Integer.valueOf(addNodeQuery.getSelectedId()));
    }

    @ApiOperation(value = "创建父级节点", notes = "为指定子节点创建一个新的父级节点")
    @PostMapping("/create-parent")
    public ComResult<?> createParentNode(@RequestBody @Validated AddNodeQuery addNodeQuery) {
        return nodesService.createParentNode(addNodeQuery.getSelectedId());
    }

    @ApiOperation(value = "删除节点", notes = "删除指定的节点，并将其子节点的层级向上移动")
    @DeleteMapping
    public ComResult<?> deleteNode(@RequestBody @Validated DeleteNodeQuery deleteNodeQuery) {
        return nodesService.deleteNode(deleteNodeQuery);
    }

    @ApiOperation(value = "复制节点并保持原有层级", notes = "复制指定节点及其子节点，并保持原有层级结构")
    @PostMapping("/copy-nodes")
    public ComResult<?> copyNodes(@RequestBody @Validated CopyNodeQuery copyNodeQuery) {
        return nodesService.copyNodes(copyNodeQuery);
    }
}



